// pages/detail/detail.js
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
const api = require("../../config/api");
import { getBarAndTitleHeight, getStorage } from "../../utils/utils"

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		pageIndex: 1, //页码
		pages: 0,  //总页码数
		isDone: false,
		defaultTab: 1,  //选择的标题 默认第一个
		tableList: [
			{ title: "发布的", type: 1 },
			{ title: "卖出的", type: 2 },
			{ title: "买到的", type: 3 },
			{ title: "收藏", type: 4 },
		],  //头部导航栏
		orderList: [],  //闲置列表
		goodsType: { 10: '待审核', 20: '已发布', 30: '已下架', 40: '已删除', 50: '已售出', 60: '已驳回', },  // 商品状态
		orderType: { 10: '待买家付款', 20: '待确认收货', 30: '已完成', 40: '已取消', }, //订单状态（卖出的）
		orderType2: { 10: '待支付', 20: '待确认收货', 30: '已完成', 40: '已取消', }, // 订单状态（买到的）
		// 信号栏高度
    ...getBarAndTitleHeight(),
	},
	// 选择导航栏
	selectTab(e) {
		this.setData({
			defaultTab: e.currentTarget.dataset.index,
			orderList: [],
			pageIndex: 1
		});
		this.getOrderList()
	},
	// 查看详情
	goDetail(e) {
		if (this.data.defaultTab == 1 || (this.data.defaultTab == 4 && e.currentTarget.dataset.item.goodsStatus == 20)) {
			wx.navigateTo({ url: `/pages/idleGosDetail/idleGosDetail?goodsId=${e.currentTarget.dataset.item.goodsId}` });
		} else if(this.data.defaultTab == 2 || this.data.defaultTab == 3) {
			wx.navigateTo({ url: `/pages/idleOrderDetail/idleOrderDetail?orderNo=${e.currentTarget.dataset.item.orderNo}` });
		}
	},
	// 删除商品按钮
	deleteGoods(e) {
		let that = this
		wx.showModal({
			title: '提示',
			content: '确定删除该商品',
			success(res) {
				if (res.confirm) {
					that.deleteGoodsSure(e.currentTarget.dataset.item.goodsId)
				} else if (res.cancel) {
					console.log('用户点击取消删除商品')
				}
			}
		})
	},
	// 确认删除商品
	async deleteGoodsSure(goodsId) {
		const res = await request.post(api.goodsDelete, {
			id: goodsId
		})
		if (res.httpStatus == 200) {
			wx.showToast({
				title: `删除成功`,
				icon: 'none'
			})
			setTimeout(() => {
				this.getOrderList()
			}, 500);
		}
	},
	// 下架商品按钮
	shelvesGoods(e) {
		let that = this
		wx.showModal({
			title: '提示',
			content: '确定下架该商品',
			success(res) {
				if (res.confirm) {
					that.shelvesGoodsSure(e.currentTarget.dataset.item.goodsId)
				} else if (res.cancel) {
					console.log('用户点击取消下架商品')
				}
			}
		})
	},
	// 确认下架商品
	async shelvesGoodsSure(goodsId) {
		const res = await request.post(api.goodsShelves, {
			id: goodsId
		})
		if (res.httpStatus == 200) {
			wx.showToast({
				title: `下架成功`,
				icon: 'none'
			})
			setTimeout(() => {
				this.getOrderList()
			}, 500);
		}
	},
	// 重新发布商品
	async releaseGoods(e) {
		const res = await request.post(api.goodsRelease, {
			id: e.currentTarget.dataset.item.goodsId
		}, { loading: false })
		if (res.httpStatus == 200) {
			wx.showToast({
				title: `发布成功`,
				icon: 'none'
			})
			setTimeout(() => {
				this.getOrderList()
			}, 500);
		}
	},
	//发布商品
	jumpIdleRelease(){
		let communityBind= getStorage('communityBind')
		if (!communityBind) {
			wx.showModal({
				content: "请先绑定小区!",
				confirmText: "去绑定",
				success: function (res) {
					if (res.confirm) {
						wx.navigateTo({
							url: `/pages/idleCommunityList/idleCommunityList`,
						});
					}
				},
			});
			return false;
		} else {
			wx.navigateTo({
				url: `/pages/idleRelease/idleRelease`,
			});
		}
	},
	// 编辑商品
	editGoods(e) {
		wx.navigateTo({ url: `/pages/idleRelease/idleRelease?goodsId=${e.currentTarget.dataset.item.goodsId}` });
	},
	// 取消收藏
	async collectionCancel(e) {
		const res = await request.post(api.cancelCollection, {
			id: e.currentTarget.dataset.item.goodsId
		})
		if (res.httpStatus == 200) {
			wx.showToast({
				title: `取消成功`,
				icon: 'none'
			})
			setTimeout(() => {
				this.getOrderList()
			}, 500);
		}
	},

	// 获得订单列表
	async getOrderList() {
		const { data } = await request.get(api.secondHandList, {
			pageIndex: this.data.pageIndex,
			pageSize: 10,
			type: this.data.defaultTab
		})
		this.setData({
			orderList:
				this.data.pageIndex === 1
					? data.records || []
					: [...this.data.orderList, ...data.records],
			pageIndex: data.current,
			pages: data.pages,
		})
		wx.stopPullDownRefresh()
	},
	// 分页代码
	bottomClick() {
		let { pageIndex, pages } = this.data;
		if (pageIndex < pages) {
			this.setData({
				pageIndex: this.data.pageIndex + 1,
			});
			this.getOrderList();
		} else {
			this.setData({
				isDone: true
			});
		}
	},
		// 展示草稿保存提示
async	showDraftModal(params){
	wx.showModal({
		title: '提示',
		content: '是否保存草稿？',
		confirmText: '保存',
		cancelText: '不保存',
		async	success(res) {
			 if (res.confirm) { //保存草稿
			 let res= await request.post(api.addTempGoods, params,{loading: true})
			 if(res.httpStatus=== 200){
				wx.showToast({
					title: '保存成功',
					icon: 'none'
				})
			 }
			} else if (res.cancel) {//删除草稿
				if(!params.id) return //没有草稿
				let res= await request.post(api.deleteTempGoods, {id: params.id},{loading: true})
				 if(res.httpStatus=== 200){
					wx.showToast({
						title: '删除成功',
						icon: 'none'
					})
				}
			}
		}
	})
},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.setData({
			pageIndex:1
		})
		this.getOrderList()
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	// 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	},
});
